#计算阶乘

def factorial(n):
    if n== 0:
        return 1
    return n * factorial(n-1)
print(factorial(0))
print(factorial(5))

'''
#通过for循环逐步累乘计算阶乘
def factorial_iterative(n):
    if n < 0:
        raise ValueError("Factorial is not defined for negative numbers.")
    result = 1
    for i in range(1,n + 1):
        result *= i
    return result
print(factorial_iterative(5))

#通过递归计算阶乘
def factorial_recursive(n):
    if n < 0:
        raise ValueError("Factorial is not defined for negative numbers.")
    if n == 0 or n == 1:
        return 1
    return n * factorial_recursive(n - 1)
print(factorial_recursive(5))

#使用内置函数math.factorial()计算阶乘
import math
def factorial_builtin(n):
    if n < 0:
        raise ValueError("Factorial is not defined for negative numbers.")
    return math.factorial(n)
print(factorial_builtin(5))

#使用reduce()函数计算阶乘
from functools import reduce
def factorial_reduce(n):
    if n < 0:
        raise ValueError("Factorial is not defined for negative numbers.")
    return reduce(lambda x, y: x * y, range(1, n + 1),1)
print(factorial_reduce(5))
'''